#include <memory>
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <cstring>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <algorithm>

#include "Daemon.hpp"
#include "InetAddr.hpp"
#include "TcpServer.hpp"
#include "Translate.hpp"

Translate trans;

void ping(int sockfd, InetAddr &addr)
{
    write(sockfd, "ok", 2);
    while (true)
    {
        char buffer[Size];
        ssize_t n = read(sockfd, buffer, sizeof buffer - 1);
        if (n > 0)
        {
            // buffer[n] = 0;

            // std::string message = '[' + addr.PrintDebug() + "] -> " + buffer;
            // std::cout << message << std::endl;

            std::string message = "server# ping >>>> ";
            write(sockfd, message.c_str(), message.size());

            // lg.LogMessage(Debug, "recive a message \n");
        }
        else if (n == 0)
        {
            lg.LogMessage(Info, "Client qiut ....\n");
            break;
        }
        else
        {
            lg.LogMessage(Error, "Read sockfd error! error code: %d , error string : %s\n", sockfd, strerror(errno));
            break;
        }
    }
}

void translate(int sockfd, InetAddr &addr)
{
    write(sockfd, "ok", 2);
    while (true)
    {
        char buffer[Size];
        int n = read(sockfd, buffer, sizeof buffer - 1);
        if (n > 0)
        {
            buffer[n] = 0;
            std::string message = buffer;
            message += "-> ";
            message += trans.excute(buffer);
            write(sockfd, message.c_str(), message.size());
        }
        else if (n == 0)
        {
            lg.LogMessage(Info, "Client qiut ....\n");
            break;
        }
        else
        {
            lg.LogMessage(Error, "Read sockfd error! error code: %d , error string : %s\n", sockfd, strerror(errno));
            break;
        }
    }
}

void transform(int sockfd, InetAddr &addr)
{
    write(sockfd, "ok", 2);
    while (true)
    {
        char buffer[Size];
        int n = read(sockfd, buffer, sizeof buffer - 1);
        if (n > 0)
        {
            buffer[n] = 0;
            std::string message = buffer;

            std::transform(message.begin(), message.end(), message.begin(), [](unsigned char ch)
                           { return std::toupper(ch); });

            write(sockfd, message.c_str(), message.size());
        }
        else if (n == 0)
        {
            lg.LogMessage(Info, "Client qiut ....\n");
            break;
        }
        else
        {
            lg.LogMessage(Error, "Read sockfd error! error code: %d , error string : %s\n", sockfd, strerror(errno));
            break;
        }
    }
}

void default_func(int sockfd, InetAddr &addr)
{
    lg.LogMessage(Debug, "default_func >>> \n");

    std::string message = "no serve !";
    write(sockfd, message.c_str(), message.size());
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
    }

    uint16_t port = std::stoi(argv[1]);

    
    // 设置守护进程
    deamon(true, false);
    
    
    std::unique_ptr<TcpServer> Tcp_server = std::make_unique<TcpServer>(port);

    // 加入服务
    Tcp_server->RegisterFunc("ping", ping);
    Tcp_server->RegisterFunc("translate", translate);
    Tcp_server->RegisterFunc("transform", transform);
    Tcp_server->RegisterFunc("default", default_func);

    // 设置日志为写入到文件中
    lg.Enable(ClassFile);  

    Tcp_server->Init();
    Tcp_server->Start();
}
